﻿using ConfigurationCommon;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

namespace RabbitMQPlugin
{
    public class RabbitMQHelper
    {
        RabbitMQServiceModel serviceModel;
        public RabbitMQHelper()
        {
            IConfiguration _configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("MQ");
            var mqServceModel = new List<RabbitMQServiceModel>();
            _configuration.Bind("Servers", mqServceModel);

            serviceModel = mqServceModel[0];
        }

        /// <summary>
        /// 服务器连接
        /// </summary>
        public IConnection GetConnection()
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.UserName = serviceModel.UserName;
            factory.Password = serviceModel.Password;
            factory.VirtualHost =serviceModel.VirtualHost;
            factory.HostName = serviceModel.HostName;

            IConnection conn = factory.CreateConnection();
            return conn;
        }

        /// <summary>
        /// 集群连接
        /// </summary>
        public IConnection ListEndPoints()
        {
            List<AmqpTcpEndpoint> endpoints = new System.Collections.Generic.List<AmqpTcpEndpoint> {
              new AmqpTcpEndpoint(){ HostName="host1"},
              new AmqpTcpEndpoint(){ HostName="host2"}
            };
            return ListEndPoints(endpoints);
        }

        /// <summary>
        /// 
        /// </summary>
        private IConnection ListEndPoints(List<AmqpTcpEndpoint> endpoints)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.UserName = serviceModel.UserName;
            factory.Password = serviceModel.Password;

            IConnection conn = factory.CreateConnection(endpoints);
            return conn;
        }

        public void SendMessageDirect(string message,string exchangeName, string queueName)
        {
            using (var con = GetConnection())
            {
                string routingkey = queueName;// "routingkey_1101";
                using (IModel channel = con.CreateModel())
                {
                    channel.ExchangeDeclare(exchangeName, ExchangeType.Direct, durable: true, autoDelete: false);
                    channel.QueueDeclare(queueName, durable: true, autoDelete: false);
                    channel.QueueBind(queueName, exchangeName, routingkey);
                    Byte[] msg = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchangeName, routingkey, null, msg);
                }
            }
        }

        public void ReciveMessage() {
            using (var con = GetConnection()) {
                string exchangeName = "exchange_1101";
                string queueName = "queue_1101";
                string routingkey = queueName;// "routingkey_1101";
                IModel channel = con.CreateModel();
                channel.ExchangeDeclare(exchangeName, ExchangeType.Direct);
                channel.QueueDeclare(queueName, false, false);
                channel.QueueBind(queueName, exchangeName, routingkey);
                var c = new EventingBasicConsumer(channel);
                c.Received += C_Received;
            }
        }

        private void C_Received(object? sender, BasicDeliverEventArgs e)
        {
            var msgbyte = e.Body.ToArray();
            var msg=Encoding.UTF8.GetString(msgbyte);
            //todo something
        }
    }
}